import { defineStore } from 'pinia';
import axios from 'axios';

interface FaultInjection {
  id: string;
  name: string;
  serviceId: string;
  serviceName: string;
  type: 'delay' | 'error' | 'drop' | 'modify';
  status: 'active' | 'inactive' | 'completed';
  config: {
    path?: string;
    method?: string;
    delay?: number;
    errorCode?: number;
    errorMessage?: string;
    dropRate?: number;
    modifyRequest?: boolean;
    modifyResponse?: boolean;
    requestModification?: any;
    responseModification?: any;
    duration?: number;
  };
  createdAt: string;
  updatedAt: string;
}

export const useFaultsStore = defineStore('faults', {
  state: () => ({
    faults: [] as FaultInjection[],
    loading: false,
    error: null as string | null,
    selectedFault: null as FaultInjection | null
  }),
  
  getters: {
    getFaultById: (state) => (id: string) => {
      return state.faults.find(fault => fault.id === id);
    },
    
    activeFaults: (state) => {
      return state.faults.filter(fault => fault.status === 'active');
    },
    
    faultsByService: (state) => (serviceId: string) => {
      return state.faults.filter(fault => fault.serviceId === serviceId);
    },
    
    faultCount: (state) => {
      return state.faults.length;
    },
    
    activeFaultCount: (state) => {
      return state.faults.filter(fault => fault.status === 'active').length;
    }
  },
  
  actions: {
    async fetchFaults() {
      this.loading = true;
      this.error = null;
      
      try {
        // 替换为实际的API端点
        const response = await axios.get('/api/faults');
        this.faults = response.data;
      } catch (err: any) {
        this.error = err.message || '获取故障注入列表失败';
        console.error('获取故障注入列表失败:', err);
      } finally {
        this.loading = false;
      }
    },
    
    async createFault(fault: Omit<FaultInjection, 'id' | 'createdAt' | 'updatedAt'>) {
      this.loading = true;
      this.error = null;
      
      try {
        // 替换为实际的API端点
        const response = await axios.post('/api/faults', fault);
        this.faults.push(response.data);
        return response.data;
      } catch (err: any) {
        this.error = err.message || '创建故障注入失败';
        console.error('创建故障注入失败:', err);
        throw err;
      } finally {
        this.loading = false;
      }
    },
    
    async updateFault(id: string, updates: Partial<FaultInjection>) {
      this.loading = true;
      this.error = null;
      
      try {
        // 替换为实际的API端点
        const response = await axios.put(`/api/faults/${id}`, updates);
        
        const index = this.faults.findIndex(fault => fault.id === id);
        if (index !== -1) {
          this.faults[index] = response.data;
        }
        
        return response.data;
      } catch (err: any) {
        this.error = err.message || '更新故障注入失败';
        console.error('更新故障注入失败:', err);
        throw err;
      } finally {
        this.loading = false;
      }
    },
    
    async deleteFault(id: string) {
      this.loading = true;
      this.error = null;
      
      try {
        // 替换为实际的API端点
        await axios.delete(`/api/faults/${id}`);
        
        const index = this.faults.findIndex(fault => fault.id === id);
        if (index !== -1) {
          this.faults.splice(index, 1);
        }
        
        if (this.selectedFault?.id === id) {
          this.selectedFault = null;
        }
      } catch (err: any) {
        this.error = err.message || '删除故障注入失败';
        console.error('删除故障注入失败:', err);
        throw err;
      } finally {
        this.loading = false;
      }
    },
    
    selectFault(fault: FaultInjection) {
      this.selectedFault = fault;
    },
    
    clearSelectedFault() {
      this.selectedFault = null;
    }
  }
});
